package com.example.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    /**
     * 格式化日期
     * @param source
     * @param Format
     * @return
     * @throws Exception
     */
    public static Long getDateMillisecond(String source,String Format) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat(Format);

        Date date= null;
        try {
            date = sdf.parse(source);
        } catch (ParseException e) {
            throw new Exception("token格式有误 ！");
        }
        return date.getTime();
    }

    /**
     * 获取时间段之间的所有日期
     * @param start
     * @param end
     * @return
     */
    public static List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd) || tempStart.equals(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

}
